package com.wngbms.netty.book._06endec._02;

import com.wngbms.netty.book._06endec._01.StringProcessHandler;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.embedded.EmbeddedChannel;

import java.nio.charset.StandardCharsets;

public class CustomerDecoderTest {

    public static void main(String[] args) {
        ChannelInitializer channelInitializer = new ChannelInitializer() {
            @Override
            protected void initChannel(Channel ch) throws Exception {
                ChannelPipeline pipeline = ch.pipeline();
                pipeline.addLast(new CustomerDecoder());
                pipeline.addLast(new StringProcessHandler());
            }
        };

        EmbeddedChannel channel = new EmbeddedChannel(channelInitializer);
        for (int i = 0; i < 1; i++) {
            ByteBuf buffer = Unpooled.buffer();
            buffer.writeBytes(new byte[]{0x01, 0x00});
            channel.writeInbound(buffer);

            buffer = Unpooled.buffer();
            buffer.writeBytes(new byte[]{0x05, 0x09});
            channel.writeInbound(buffer);

            byte[] bytes = "abcde".getBytes(StandardCharsets.UTF_8);
            buffer = Unpooled.buffer();
            buffer.writeBytes(bytes);
//            channel.writeAndFlush(buffer);
//            channel.flush();
            channel.writeInbound(buffer);
        }
    }

}
